//
//  HookAPI.m
//  Staff
//
//  Created by caiyao on 1/13/15.
//
//

#import "HookAPI.h"

@implementation HookAPI

- (id)initWithHookCommand:(HookCommand *)hookCommand webView:(UIWebView *)webView hookViewController:(HookWebViewController *)controller {
    if (self = [super init]) {
        self.command = hookCommand;
        self.webView = webView;
        self.hookViewController = controller;
        self.excuteStatus = HookAPI_EXCUTE_STATUS_READY;
    }
    return self;
}

- (void)excute {
    
}

- (void)excuteNonHookFoundCallback {
    NSString *jsFailCallBack = [NSString stringWithFormat:region_webview_statusCall,
                                region_webview_hookAPIFailCall,
                                CNS_STATUS_HOOKAPI_ERR];
    
    DebugLog(@"jsFailCallBack is %@",jsFailCallBack);
    
    [self.webView stringByEvaluatingJavaScriptFromString:jsFailCallBack];
    [[NSNotificationCenter defaultCenter] postNotificationName:COMMAND_EXCUTE_DONE_NOTIFICATION object:self];
}

- (void)excuteCallBackJSWithParameters:(NSString *)params {
    NSString *jsCall;
    if (params == nil) {
        jsCall = [NSString stringWithFormat:@"%@();",self.command.callbackJS];
    } else {
        jsCall = [NSString stringWithFormat:@"%@('%@');",self.command.callbackJS, params];
    }
    DebugLog(@"js call %@",jsCall);
    
    DebugLog(@"%@",[self.webView stringByEvaluatingJavaScriptFromString:jsCall]);
    [[NSNotificationCenter defaultCenter] postNotificationName:COMMAND_EXCUTE_DONE_NOTIFICATION object:self];
}

@end
